package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import vo.ListUser;

public class ListUserDao {
	
	private PreparedStatement ptmt=null;
	private Statement pmt=null;
	
	public ListUserDao(){}
	
	public ListUser chaXun(Connection con,String id) throws Exception{
		
         try{
        	ListUser resultlistUser=null;
			String sql="select * from consumer where id=?";
			Statement pmt=con.createStatement();
			ResultSet rs=pmt.executeQuery(sql);
			
			while(rs.next()){
				resultlistUser=new ListUser();
				resultlistUser.setId(rs.getString(1));
				resultlistUser.setPassword(rs.getString(2));
				resultlistUser.setName(rs.getString(3));
				resultlistUser.setSex(rs.getString(4));
				resultlistUser.setMethord(rs.getString(5));
				resultlistUser.setJob(rs.getString(6));
				resultlistUser.setCompany(rs.getString(7));
				resultlistUser.setProvince(rs.getString(8));
				resultlistUser.setTelephone(rs.getString(9));
				resultlistUser.setMail(rs.getString(10));
				resultlistUser.setMailaddress(rs.getString(11));
				resultlistUser.setPost_code(rs.getString(12));
				resultlistUser.setDate(rs.getString(13));
				resultlistUser.setState(rs.getString(14));
				
			}
			return resultlistUser;
			
		}finally{
			if(null!=pmt){
				pmt.close();
			}
			if(null!=con){
				con.close();
			}
		}
		
		
	}
	
	/*public ListUser getListUser(Connection con) throws Exception{
		
		ListUser resultlistUser=null;
		String sql="select state,id,name,telephone,mail,date from consumer";
		Statement ptmt=con.createStatement();
		ResultSet rs=ptmt.executeQuery(sql);
		while(rs.next()){
			resultlistUser=new ListUser();
			resultlistUser.setId(rs.getString(1));
			resultlistUser.setName(rs.getString(2));
			resultlistUser.setTelephone(rs.getString(3));
			resultlistUser.setMail(rs.getString(4));
			resultlistUser.setDate(rs.getString(5));
			resultlistUser.setState(rs.getString(6));
			
		}
		return resultlistUser;
	}*/
	public void delete(Connection con,String id) throws Exception{
		
		try{
			String sql="delete from consumer where id=?";
			ptmt=con.prepareStatement(sql);
			ptmt.setString(1, id);
			ptmt.executeUpdate();
		}finally{
			if(null!=ptmt){
				ptmt.close();
			}
			if(null!=con){
				con.close();
			}
		}
		
	}
	
	public void add(Connection con,String loginName,String loginPassword,String userName,String sex,String paymentStyle,String career,String company,String nationality,String userPhone,String userEmail,String mailAddress,String postCode,String kaihudate,String userStatus) throws Exception{ 
		
		try{
			
			String sql="insert into consumer "+"(id,password,name,sex,methord,job,company,province,telephone,mail,mailaddress,post_code,date,state)"+"values("+"'"+loginName+"'"+
				","+"'"+loginPassword+"'"+","+"'"+userName+"'"+","+"'"+sex+"'"+","+"'"+
				paymentStyle+"'"+","+"'"+career+"'"+","+"'"+company+"'"+","+"'"+
				nationality+"'"+","+"'"+userPhone+"'"+","+"'"+userEmail+"'"+","+"'"+
				mailAddress+"'"+","+"'"+postCode+"'"+","+"'"+kaihudate+"'"+","+"'"+userStatus+"'"+")";
			pmt=con.createStatement();
			pmt.executeUpdate(sql);
			pmt.close();
			con.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
}
